﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dryricenet
{
    public partial class home_Evaluation : System.Web.UI.Page
    {
        public int id = 0;
        public int userid = 0;
        public string Ordertitle = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               Ordertitle= Request.QueryString["Ordertitle"].ToString();
                if (OrderBLL.orderSHBLL(Ordertitle).ToString()!= "已收货")
                {
                    Response.Write("<script>javascript:alert('亲!您的美食还没有签收，是不能评价的');history.back();</script>");
                    Response.End();
                    Response.Redirect("home_Orderview.aspx");
                }
                id = Convert.ToInt32(Request.QueryString["FoodID"].ToString());
                DataList1.DataSource = foodBLL.selectIdBLL(id);
                DataList1.DataBind();
                string name = Session["Name"].ToString();
                DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                userid= Convert.ToInt32(dr["UserID"]);

            }
        }
        //提价评价
        protected void Button1_Click(object sender, EventArgs e)
        {
            Ordertitle = Request.QueryString["Ordertitle"].ToString();
            id = Convert.ToInt32(Request.QueryString["FoodID"].ToString());
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            userid = Convert.ToInt32(dr["UserID"]);
            if (AppraiseBLL.appraisePCBLL(id, userid, Ordertitle).Rows.Count > 0)
            {
                Response.Write("<script>javascript:alert('亲!您已经评价过此商品了哦！');history.back();</script>");
                Response.End();
            }
            else {
                Appraise a = new Appraise();
                a.FoodID = id;
                a.UserID = userid;
                if (fudAppurl.HasFile)
                {
                    if (CheckFileType(fudAppurl.FileName))
                    {
                        string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + Path.GetExtension(fudAppurl.FileName);
                        string filePath = "~/Evaluationimg/" + fileName;
                        fudAppurl.SaveAs(MapPath(filePath));
                        a.Appurl = filePath;
                    }
                }
                else {
                    a.Appurl = "";
                }
                a.Content = txtContent.Text.Trim();
                a.Time = DateTime.Now;
                a.Score = txtScore.Text.Trim();
                a.Rider = txtRider.Text.Trim();
                a.Ordertitle = Ordertitle;
                if (CheckBox1.Checked)
                {
                    a.Anonymous = "（匿名）";
                }
                if (txtScore.Text.Trim() == "" && txtRider.Text.Trim() == "" && txtContent.Text.Trim() == "")
                {
                    Response.Write("<script>javascript:alert('亲!请将评分和评价信息填写完整！');history.back();</script>");
                    Response.End();
                }
                else if (Convert.ToInt32(txtScore.Text.Trim())<0 && Convert.ToInt32(txtScore.Text.Trim()) > 5 && Convert.ToInt32(txtRider.Text.Trim())<0 && Convert.ToInt32(txtRider.Text.Trim()) > 5)
                {
                    Response.Write("<script>javascript:alert('亲!请按评分规则给分哦！');history.back();</script>");
                    Response.End();
                }
                else
                {
                    if (AppraiseBLL.appraiseAddBLL(a))
                    {
                        btnPJ.Enabled = false;
                        Response.Write("<script>javascript:alert('亲!感谢您的评价！请继续购物吧。');history.back();</script>");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('真遗憾您的评价失败了！');history.back();</script>");
                        Response.End();
                    }
                   
                }
            }
            
            
        }
        public bool CheckFileType(string filename)
        {
            string ext = Path.GetExtension(filename);
            switch (ext.ToLower())
            {
                case ".gif": return true;
                case ".png": return true;
                case ".jpg": return true;
                case ".jpeg": return true;
                default: return false;
            }
        }
    }
}